package cn.zjweu.dao.impl;

import cn.zjweu.dao.CategoryDao;
import cn.zjweu.domain.DishCategory;
import cn.zjweu.domain.MessageInfo;
import cn.zjweu.utils.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CategoryDaoImpl implements CategoryDao {
    private Connection conn;
    private PreparedStatement pst;
    private ResultSet rs;

    private void initThree() throws SQLException {
        conn = JDBCUtil.getConnection();
        pst = null;
        rs = null;
    }

    private void initTwo() throws SQLException {
        conn = JDBCUtil.getConnection();
        pst = null;
    }

    @Override
    public List<DishCategory> findCategory() {
        String sql = "SELECT * FROM dishCategory";
        List<DishCategory> categoryList = new ArrayList<>();
        try {
            initThree();
            pst = conn.prepareStatement(sql);
            System.out.println(pst);
            rs = pst.executeQuery();
            while (rs.next()) {
                DishCategory dishCategory = new DishCategory();
                dishCategory.setCategoryId(rs.getInt(1));
                dishCategory.setCategoryName(rs.getString(2));
                categoryList.add(dishCategory);
            }
            return categoryList;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(rs, pst, conn);
        }
        return null;
    }

    @Override
    public DishCategory findCategoryById(int categoryId) {
        String sql = "SELECT * FROM dishCategory WHERE categoryId=?";
        DishCategory dishCategory = null;
        try {
            initThree();
            pst = conn.prepareStatement(sql);
            pst.setInt(1, categoryId);
            System.out.println(pst);
            rs = pst.executeQuery();
            while (rs.next()) {
                dishCategory = new DishCategory();
                dishCategory.setCategoryId(rs.getInt(1));
                dishCategory.setCategoryName(rs.getString(2));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(rs, pst, conn);
        }
        return dishCategory;
    }

    @Override
    public void editCategoryById(int categoryId, String categoryName) {
        String sql = "UPDATE dishCategory SET categoryName = ? WHERE categoryId = ?";

        try {
            initTwo();
            pst = conn.prepareStatement(sql);
            pst.setString(1, categoryName);
            pst.setInt(2, categoryId);
            System.out.println(pst);
            pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(pst, conn);
        }
    }

    @Override
    public boolean delCategoryById(int categoryId) {
        String sql = "DELETE FROM dishCategory WHERE categoryId = ?";

        try {
            initTwo();
            pst = conn.prepareStatement(sql);
            pst.setInt(1, categoryId);
            System.out.println(pst);
            pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            return MessageInfo.FAIL;
        } finally {
            JDBCUtil.close(pst, conn);
        }
        return MessageInfo.SUCCESS;
    }

    @Override
    public boolean addCategory(String categoryName) {
        String sql = "INSERT INTO dishCategory (categoryName) VALUES (?)";
        try {
            initTwo();
            pst = conn.prepareStatement(sql);
            pst.setString(1,categoryName);
            System.out.println(pst);
            pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
